from django.conf import settings
from django.contrib.auth import authenticate
from django.contrib.auth import login
from django.contrib.sites.models import Site
from django.contrib.auth.models import User


from registration import signals
from registration.forms import RegistrationForm, RegistrationFormUniqueEmail
from registration.models import RegistrationProfile


class UsecaseBackend(object):
    """
    A registration backend which implements the simplest possible
    workflow: a user supplies a username, email address and password
    (the bare minimum for a useful account), and is immediately signed
    up and logged in.
    
    """
    def register(self, request, **kwargs):
        """
        Create and immediately log in a new user.
        
        """
        fname, lname, email, password = kwargs['fname'], kwargs['lname'], kwargs['email'], kwargs['password1']
        username = email
        if Site._meta.installed:
            site = Site.objects.get_current()
        else:
            site = RequestSite(request)
            
        # does an equivalent user profile already exist?
        try:
            User.objects.get(username=username)
        except User.DoesNotExist:
            pass            # all good, they shouldnt exist
        else:
            return None     # they already exist
            
        new_user = RegistrationProfile.objects.create_inactive_user(username, email,
                                                                    password, site)
        new_user.first_name = fname
        new_user.last_name = lname
        new_user.save()
        
        signals.user_registered.send(sender=self.__class__,
                                     user=new_user,
                                     request=request)
        return new_user


    def activate(self, request, activation_key):
        """
        Given an an activation key, look up and activate the user
        account corresponding to that key (if possible).

        After successful activation, the signal
        ``registration.signals.user_activated`` will be sent, with the
        newly activated ``User`` as the keyword argument ``user`` and
        the class of this backend as the sender.
        
        """
        activated = RegistrationProfile.objects.activate_user(activation_key)
        if activated:
            signals.user_activated.send(sender=self.__class__,
                                        user=activated,
                                        request=request)
        return activated



    def registration_allowed(self, request):
        """
        Indicate whether account registration is currently permitted,
        based on the value of the setting ``REGISTRATION_OPEN``. This
        is determined as follows:

        * If ``REGISTRATION_OPEN`` is not specified in settings, or is
          set to ``True``, registration is permitted.

        * If ``REGISTRATION_OPEN`` is both specified and set to
          ``False``, registration is not permitted.
        
        """
        return getattr(settings, 'REGISTRATION_OPEN', True)

    def get_form_class(self, request):
        return RegistrationFormUniqueEmail

    def post_registration_redirect(self, request, user):
        """
        After registration, redirect to the user's account page.
        
        """
        return ('registration_complete', (), {})

    def post_activation_redirect(self, request, user):
        #raise NotImplementedError
        return ('registration_activation_complete', (), {})
